local ModalDialog = require("app.widget.ModalDialog")

local ConfirmPayDialog = class("ConfirmPayDialog",function() return ModalDialog.new() end)


function ConfirmPayDialog:ctor(textContent1,onConfirm,onCancel,textContent2,payType,payAmount,buyAmount)

    local Util = require("app.utils.Util")

    self.onConfirmCB = onConfirm
    self.onCancelCB = onCancel

    self.mUINode = cc.uiloader:load(Res.ConfirmPayDialogUI) 
    self:addChild(self.mUINode)
    self.mUINode:setContentSize(cc.size(display.width,display.height))
    ccui.Helper:doLayout(self.mUINode)

    self:playUIAction()

    --Res.Currency_Icon.coin
    self.payTypeIcon = self.mUINode:getChildByName("tips"):getChildByName("zuanshi"):getChildByName("Icon_zuanshi")
    if self.payTypeIcon then
        local icon=GD:getResIcon(payType)
        if icon then
            self.payTypeIcon:loadTexture(icon)
        end
        if payAmount then
            self.payTypeIcon:runAction(cc.MoveBy:create(0,cc.p(4*(9-string.len(payAmount)),0)))
        end
    end

    self.payAmountLabel = self.mUINode:getChildByName("tips"):getChildByName("zuanshi"):getChildByName("num")
    if self.payAmountLabel then
        if payAmount>UD:getGold() then
            --钱不够
            self.payAmountLabel:setColor(cc.c4b(255,0,0,255)) 
        else
            self.payAmountLabel:setColor(cc.c4b(0,194,223,255)) 
        end
        self.payAmountLabel:setString(payAmount) 
        if payAmount then
            self.payAmountLabel:runAction(cc.MoveBy:create(0,cc.p(4*(9-string.len(payAmount)),0)))
        end
    end

    self.textContent1 = self.mUINode:getChildByName("tips"):getChildByName("tlt")
    if self.textContent1 then
        if buyAmount then
            self.textContent1:setString(string.format(textContent1,buyAmount)) 
        else
            self.textContent1:setString(textContent1) 
        end
    end

    self.textContent2 = self.mUINode:getChildByName("tips"):getChildByName("tips_txt")
    if self.textContent2 then
        self.textContent2:setString(textContent2) 
    end
    
    local confirmButton = self.mUINode:getChildByName("tips"):getChildByName("Button_queding")
    if confirmButton then
        self.confirmButton = confirmButton
        confirmButton:addClickEventListener(function(sender)
             if self.onConfirmCB then
                self.onConfirmCB(self)
            end 
        end) 
    end

    local cancelButton = self.mUINode:getChildByName("tips"):getChildByName("Button_quxiao")
    if cancelButton then
        self.cancelButton = cancelButton
        cancelButton:addClickEventListener(function(sender)
            if self.onCancelCB then
                self.onCancelCB(self)
            end 
        end)
    end
end

function ConfirmPayDialog:playUIAction()
    -- play ui action
    local uiAction = cc.CSLoader:createTimeline(Res.ConfirmPayDialogUI)
    self.mUINode:runAction(uiAction)
    -- uiAction:gotoFrameAndPlay(0,uiAction:getDuration(),false)
    uiAction:play("fadein",false)
    uiAction:setLastFrameCallFunc(function()
        
    end)
end

function ConfirmPayDialog:setTextContent1(c)
    if self.textContent1 and c then
        self.textContent1:setString(c)
    end
end

function ConfirmPayDialog:setTextContent2(c)
    if self.textContent2 and c then
        self.textContent2:setString(c)
    end
end

-- function ConfirmPayDialog:setConfirmText(text)
--     if self.confirmButton and text then
--         self.confirmButton:setTitleText(text)
--     end
-- end

-- function ConfirmPayDialog:setCancelText(text)
--     if self.cancelButton and text then
--         self.cancelButton:setTitleText(text)
--     end
-- end

return ConfirmPayDialog